package com.lijing.binarySearch;

import java.util.ArrayList;
import java.util.List;

public class BinarySearch2 {
    public static void main(String[] args) {
        int[] arr = {1,3,4,6,6,7,8,8,10};
        List<Integer> ls = new ArrayList<Integer>();
        ls = binarySearch2(arr,0,arr.length-1,8);
        System.out.println(ls);

    }

    private static List<Integer> binarySearch2(int[] arr, int left, int right, int findVal){
        if(left > right){
            return null;
        }

        int mid = (left + right)/2;
        int midVal = arr[mid];

        if(findVal > midVal){
            return binarySearch2(arr, mid+1, right,findVal);
        }else if(findVal < midVal){
            return binarySearch2(arr,left,mid-1,findVal);
        }else {
            List<Integer> result = new ArrayList<Integer>();
            int temp = mid -1;
            while (temp >= 0 && arr[temp] == findVal) {
                result.add(temp);
                temp--;
            }
            result.add(mid);
            temp = mid +1;
            while (temp<=arr.length && arr[temp]==findVal){
                result.add(temp);
                temp++;
            }
            return result;
        }

    }
}
